﻿using CSharpChores415.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using CSharpChores415.Data;
using CSharpChores.Data;

namespace CSharpChores415.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //Create vm
            KidsIndexVm KvM = new KidsIndexVm();
            //populate vm
            using (ApplicationDbContext db = new ApplicationDbContext())
            //data type  variable name  instantiate it 
            {
                KvM.ChoreList = db.Chores.ToList();
                KvM.KidList = db.Kids.ToList();
                List<KidsChoreIndexVm> Kc = new List<KidsChoreIndexVm>();
                Kc = db.Chores.Select(c => new KidsChoreIndexVm()
                {
                    ChoreName = c.ChoreName,
                    KidList = db.KidChores.Where(n => n.ChoreId == c.ChoreId).Select(kc=>new KidVm()
                    {
                        KidName = kc.Kid.KidName

                    }).ToList()
                }).ToList(); //binds using lookup (many to many) table
                KvM.AllChoresWithKids = Kc;
                //send vm to view
                return View(KvM);

            }
            

        }

        // Show all chores for kid X
        public ActionResult ChoreForKid(int idk)
        {
            ChoreDetailsVm dVm = new ChoreDetailsVm(); // create vm
            // db call // populate vm
            using (ApplicationDbContext data = new ApplicationDbContext() )
            {
                dVm.Name = data.Kids.FirstOrDefault(k => k.KidId == idk).KidName;
                dVm.AssignedChores = data.KidChores.Where(c => c.KidId == idk)
                    .Select(ch => new ChoreVm()
                    {
                        Name = ch.Chore.ChoreName
                    }).ToList();
                 return View(dVm);   // send vm to view
            }
        }

        // show all kids for chore X
        public ActionResult KidForChore(int cid)
        {
            KidChoreVm dVm = new KidChoreVm(); // create vm
            using (ApplicationDbContext data = new ApplicationDbContext())
            {    // populate
                dVm.ChoreName = data.Chores.FirstOrDefault(k => k.ChoreId == cid).ChoreName;
                dVm.KidList = data.KidChores.Where(k => k.ChoreId == cid)
                    .Select(nk => new KidVm()
                    {
                        KidName = nk.Kid.KidName
                    }).ToList();
                // send to view
                return View(dVm);
            }


            
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}